/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache license, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the license for the specific language governing permissions and
 * limitations under the license.
 */
package com.hsh;

import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.pattern.ConverterKeys;
import org.apache.logging.log4j.core.pattern.LogEventPatternConverter;
import org.apache.logging.log4j.util.PerformanceSensitive;


/**
 * Description: 输出宙斯调用链ID
 *
 * @author 冯东宝（dongbao.feng@ucarinc.com）
 * @date 2019年08月05日19:59:07
 * @since 1.0
 */

@Plugin(name = "ZeusIdPatternConverter", category = "Converter")
@ConverterKeys({"Z", "zeusId"})
@PerformanceSensitive("allocation")
public final class ZeusIdPatternConverter extends LogEventPatternConverter {


    /**
     * Singleton.
     */
    private static final ZeusIdPatternConverter INSTANCE = new ZeusIdPatternConverter(new String[0]);

    public static ZeusIdPatternConverter newInstance(final String[] options) {
        return INSTANCE;
    }

    /**
     * 构造方法
     */
    private ZeusIdPatternConverter(final String... options) {
        super("Zeus ID", "zid");
    }


    /**
     * 对输入内容进行处理-追加宙斯trace_id
     */
    @Override
    public void format(final LogEvent event, final StringBuilder toAppendTo) {
        toAppendTo.append("[ZEUSID ");
        toAppendTo.append(event.getContextMap().get("zeusId"));
        toAppendTo.append("]");
    }
}
